﻿Public Class SelectQuizControl
    Dim _Starting As Boolean = False
    Public Sub Start()
        _Starting = True
        CType(Parent, GroupBox).Text = "Quiz Console"
        Parent.Controls.Remove(Me)
        Me.Dispose()
    End Sub

    Private Sub SelectQuizControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        MPQClient.Server.GetQuizFiles()
        If FirstPlay Then
            TutorialControl1.ShowAndMoveOn()
        End If
        CType(Parent, GroupBox).Text = "Select Quiz File"
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If Not ListBox1.SelectedItem = "" Then
            CType(CType(ParentForm, MainForm).MainPanel.Controls(0), WelcomeControl).HostButton.Enabled = True
        End If
    End Sub

    Private Sub UploadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadButton.Click
        If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim UploadThread As New Threading.Thread(AddressOf _UploadThread)
            UploadThread.Start()
        End If
    End Sub

    Sub _UploadThread()
        Dim Name As String = OpenFileDialog.SafeFileName
checkname: If ListBox1.Items.Contains(Name) Then
            Name = InputBox("Server already contains a file with this name. Please enter a new one.")
            If Not Name.EndsWith(".mqf") Then Name += ".mqf"
            GoTo checkname
        End If
        If Name = "" Then Exit Sub
        Dim stream As New IO.FileStream(OpenFileDialog.FileName, IO.FileMode.Open)
        Dim comp As New System.IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Decompress)
        Dim SR As New IO.StreamReader(comp)

        Dim Quiz As String = SR.ReadToEnd
        Const Partlength = 4096
        Dim TotalParts As Integer = Math.Ceiling(Quiz.Length / Partlength) - 1

        Invoke(SetProgressBarVisible, True)
        Invoke(SetProgressBarMaximum, TotalParts)

        For i As Integer = 0 To TotalParts
            Invoke(SetProgressBarValue, i)
            Try
                MPQClient.Server.UploadQuizFile(Quiz.Substring(i * Partlength, Partlength), Name, i, TotalParts)
            Catch
                MPQClient.Server.UploadQuizFile(Quiz.Substring(i * Partlength), Name, i, TotalParts)
            End Try
            If i = 0 Then System.Threading.Thread.Sleep(1000)
        Next

        Invoke(SetProgressBarVisible, False)

        SR.Close()
        comp.Close()
        stream.Close()
    End Sub

    Dim SetProgressBarValue As New SafeSetProgressBarValue(AddressOf _SetProgressBarValue)
    Delegate Sub SafeSetProgressBarValue(ByVal Value As Integer)
    Sub _SetProgressBarValue(ByVal Value As Integer)
        Try
            ProgressBar.Value = Value
        Catch
        End Try
    End Sub

    Dim SetProgressBarMaximum As New SafeSetProgressBarMaximum(AddressOf _SetProgressBarMaximum)
    Delegate Sub SafeSetProgressBarMaximum(ByVal Maximum As Integer)
    Sub _SetProgressBarMaximum(ByVal Maximum As Integer)
        Try
            ProgressBar.Maximum = Maximum
        Catch
        End Try
    End Sub

    Dim SetProgressBarVisible As New SafeSetProgressBarVisible(AddressOf _SetProgressBarVisible)
    Delegate Sub SafeSetProgressBarVisible(ByVal Visible As Boolean)
    Sub _SetProgressBarVisible(ByVal Visible As Boolean)
        Try
            If Visible = True Then
                ProgressBar.Show()
            Else
                ProgressBar.Hide()
            End If
        Catch
        End Try
    End Sub

    Private Sub RefreshTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
        MPQClient.Server.GetQuizFiles()
    End Sub
End Class
